const http = require('http');
//读模块
const fs = require('fs');
//处理路径
const path = require('path');

/* 封装 */
let staticServices = (req,res,rootPath) => {
    //读
    fs.readFile(path.join(rootPath,req.url),(err, data) => {
        if(err){
            console.log("出错啦!!!");
            res.writeHead(200,{"Content-Type":"text/plain;charset=utf-8"})
            res.end('500 服务器开小差啦！！！程序员小哥正在维护...');
        }else{
            res.write(data);
            res.end()
        }
    })
}
/* __dirname当前路径下 */
let root = path.join(__dirname,'WWW');

//创建服务
http.createServer((req,res) => {


    if(req.url === '/index.html'){
        staticServices(req,res,root);

    }else if(req.url === '/index.css'){
        staticServices(req,res,root);
    }else if(req.url === '/index.js'){
        staticServices(req,res,root);
    }else{
        res.end('404');
    }
}).listen(8080,() => {
    console.log(' --->','服务器已启动！！');
})